package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.*;
import com.coding.pojo.*;
import com.coding.service.HistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/31
 **/
@Service
public class HistoryImpl implements HistoryService {
    @Autowired
    HistoryDao historyDao;
    @Autowired
    PostDao postDao;
    @Autowired
    MainPlaceDao mainPlaceDao;
    @Autowired
    PoemDao poemDao;
    @Autowired
    UserDao userDao;

    @Override
    public void addnewhistorypost(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Post post = postDao.selectByPrimaryKey(source_id);
        if (post.getPost_title() == null) {
            baseModel.setFailure("查无此帖子资源");
            return;
        }
        Userhistorypost userhistorypost = historyDao.selectPostSingle(source_id,user_id);
        if (userhistorypost != null) {
            if (historyDao.updateHistory(user_id, source_id, "post", DateUtil.getStringDate()) == 1) {
                Userhistorypost newHistory = historyDao.selectPostSingle(source_id,user_id);
                if (newHistory != null) {
                    baseModel.setSucceed("更新历史成功", newHistory);
                    return;
                }
            }

        }
        History history = new History();
        history.setUser_id(user_id);
        history.setSource_id(source_id);
        history.setHistory_kind("post");
        history.setHistory_time(DateUtil.getStringDate());

        if (historyDao.insertSelective(history) == 1) {
            baseModel.setSucceed("成功添加帖子历史", history);
        } else {
            baseModel.setFailure("添加帖子历史失败");
        }
    }

    @Override
    public void gethistorypost(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Userhistorypost> userhistorypostList = historyDao.selectpost(user_id);
        PageInfo pageInfo = new PageInfo(userhistorypostList);
        userhistorypostList = pageInfo.getList();
        if (userhistorypostList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的记录");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的记录共" + userhistorypostList.size() + "条", userhistorypostList);
    }

    @Override
    public void addnewhistoryplace(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        MainPlace mainPlace = mainPlaceDao.selectByPrimaryKey(source_id);
        if (mainPlace == null) {
            baseModel.setFailure("查无此景点资源");
            return;
        }
        Userhistoryplace userhistoryplace = historyDao.selectMainplaceSingle(source_id, user_id);
        if (userhistoryplace != null) {
            if (historyDao.updateHistory(user_id, source_id, "mainplace", DateUtil.getStringDate()) == 1) {
                Userhistoryplace userHistory = historyDao.selectMainplaceSingle(source_id, user_id);
                if (userHistory != null) {
                    baseModel.setSucceed("更新历史成功", userHistory);
                    return;
                }
            }
        }
        History history = new History();
        history.setUser_id(user_id);
        history.setSource_id(source_id);
        history.setHistory_kind("mainplace");
        history.setHistory_time(DateUtil.getStringDate());
        if (historyDao.insertSelective(history) == 1) {
            baseModel.setSucceed("成功添加景点收藏", history);
        } else {
            baseModel.setFailure("添加景点收藏失败");
        }
    }

    @Override
    public void gethistoryplace(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Userhistoryplace> userhistoryplaceList = historyDao.selectMainplace(user_id);
        PageInfo pageInfo = new PageInfo(userhistoryplaceList);
        userhistoryplaceList = pageInfo.getList();
        if (userhistoryplaceList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的历史记录");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的历史记录共" + userhistoryplaceList.size() + "条", userhistoryplaceList);
    }

    @Override
    public void addNewHistoryPoem(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Poem poem = poemDao.selectByPrimaryKey(source_id);
        if (poem == null) {
            baseModel.setFailure("查无此诗词资源");
            return;
        }
        Userhistorypoem userhistorypoem = historyDao.selectPoemSingle(source_id, user_id);
        if (userhistorypoem != null) {
            if (historyDao.updateHistory(user_id, source_id, "poem", DateUtil.getStringDate()) == 1) {
                Userhistorypoem newHistory = historyDao.selectPoemSingle(source_id, user_id);
                if (newHistory!= null) {
                    baseModel.setSucceed("更新历史成功", newHistory);
                    return;
                }
            }
        }
        History history = new History();
        history.setUser_id(user_id);
        history.setSource_id(source_id);
        history.setHistory_kind("poem");
        history.setHistory_time(DateUtil.getStringDate());
        if (historyDao.insertSelective(history) == 1) {
            baseModel.setSucceed("成功添加诗词历史", history);
        } else {
            baseModel.setFailure("添加诗词历史失败");
        }
    }

    @Override
    public void getHistoryPoem(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Userhistorypoem> userhistorypoemList = historyDao.selectpoem(user_id);
        PageInfo pageInfo = new PageInfo(userhistorypoemList);
        userhistorypoemList = pageInfo.getList();
        if (userhistorypoemList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的历史记录");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的历史记录共" + userhistorypoemList.size() + "条", userhistorypoemList);
    }

    @Override
    public void getAllHistory(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        UserhistoryAll userhistoryAll = new UserhistoryAll();
        List<Userhistorypost> userhistorypostList = historyDao.selectpost(user_id);
        List<Userhistoryplace> userhistoryplaceList = historyDao.selectMainplace(user_id);
        List<Userhistorypoem> userhistorypoemList = historyDao.selectpoem(user_id);
        if (userhistorypostList != null) {
            userhistoryAll.setUserhistorypostList(userhistorypostList);
        }
        if (userhistoryplaceList != null) {
            userhistoryAll.setUserhistoryplaceList(userhistoryplaceList);
        }
        if (userhistorypoemList != null) {
            userhistoryAll.setUserhistorypoemList(userhistorypoemList);
        }
        if (userhistoryAll != null) {
            baseModel.setSucceed("成功获取用户历史记录", userhistoryAll);
        } else {
            baseModel.setFailure("获取用户历史记录失败");
        }
    }

    @Override
    public void getHistory(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        List<UserHistory> userHistoryList = new ArrayList<>();
        List<History> historyList = historyDao.selectByTime(user_id);
        if (historyList == null) {
            baseModel.setFailure("该用户暂无使用记录");
            return;
        }
        int i = 1;
        for (History history : historyList
        ) {
            if (history.getHistory_kind().equals("mainplace")) {
                Userhistoryplace userhistoryplace = historyDao.selectMainplaceSingle(history.getSource_id(),user_id);
                UserHistory userHistory = new UserHistory(i, user_id, user.getUser_name(), history.getSource_id(), history.getHistory_kind(), history.getHistory_time(), userhistoryplace.getOssImages().getImage_url());
                userHistory.setMainPlace(userhistoryplace.getMainPlace());
                userHistoryList.add(userHistory);
                i++;
                continue;
            }
            if (history.getHistory_kind().equals("post")) {
                Userhistorypost userhistorypost = historyDao.selectPostSingle(history.getSource_id(),user_id);
                UserHistory userHistory = new UserHistory(i, user_id, user.getUser_name(), history.getSource_id(), history.getHistory_kind(), history.getHistory_time(), userhistorypost.getOssImages().getImage_url());
                userHistory.setPost(userhistorypost.getPost());
                userHistoryList.add(userHistory);
                i++;
                continue;
            }
            if (history.getHistory_kind().equals("poem")) {
                Userhistorypoem userhistorypoem = historyDao.selectPoemSingle(history.getSource_id(),user_id);
                UserHistory userHistory = new UserHistory(i, user_id, user.getUser_name(), history.getSource_id(), history.getHistory_kind(), history.getHistory_time(), userhistorypoem.getOssImages().getImage_url());
                userHistory.setPoem(userhistorypoem.getPoem());
                userHistoryList.add(userHistory);
                i++;
                continue;
            }
        }
        baseModel.setSucceed("成功获得用户历史记录共" + (i - 1) + "条", userHistoryList);
    }
}
